/*
 * Licensed to the Apache Software Foundation (ASF) under one
 * or more contributor license agreements.  See the NOTICE file
 * distributed with this work for additional information
 * regarding copyright ownership.  The ASF licenses this file
 * to you under the Apache License, Version 2.0 (the
 * "License"); you may not use this file except in compliance
 * with the License.  You may obtain a copy of the License at
 *
 *   http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing,
 * software distributed under the License is distributed on an
 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
 * KIND, either express or implied.  See the License for the
 * specific language governing permissions and limitations
 * under the License.
 */

/*!
 * \file utils.h
 * \brief The util function for dtensor infer struct info
 */

#ifndef TVM_RELAX_OP_DISTRIBUTED_UTILS_H_
#define TVM_RELAX_OP_DISTRIBUTED_UTILS_H_

#include <tvm/arith/analyzer.h>
#include <tvm/relax/distributed/axis_group_graph.h>
#include <tvm/relax/op_attr_types.h>
#include <tvm/relay/expr.h>
#include <tvm/relay/op.h>

#include "../op_common.h"

namespace tvm {
namespace relax {
namespace distributed {

/*!
 * \brief Get the dtensor struct info of the operator input.
 * \param call The context Call to the operator.
 * \param ctx The error reporting context.
 * \return The dtensor struct info of each input.
 * \note This function require every input tensor to be DTensor.
 */
Array<distributed::DTensorStructInfo> GetInputDTensorStructInfo(const Call& call,
                                                                const BlockBuilder& ctx);

/*!
 * \brief Perform a local sharding spec propagation to infer the output dtensor
          struct info or tuple of dtensor struct info.
 *
 * \param call The context Call to the operator.
 * \param ctx The error reporting context.
 * \param output_sinfo The original output struct info
 * \param f_build_graph The function to build axis graph
 * \return The inferred output struct info
 */
StructInfo InferShardingSpec(const Call& call, const BlockBuilder& ctx,
                             const StructInfo& orig_output_sinfo,
                             distributed::FBuildAxisGraph f_build_graph);

}  // namespace distributed
}  // namespace relax
}  // namespace tvm

#endif  // TVM_RELAX_OP_DISTRIBUTED_UTILS_H_
